package org.canx.service;

import org.apache.log4j.Logger;
import org.canx.dao.GUserMapper;
import org.canx.model.GUser;
import org.canx.utils.Response;
import org.canx.utils.ResultHttpStatus;
import org.canx.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Enough Wang
 * @class description :
 * @time 2017年7月25日
 */
@Service
public class LoginService {

    private static Logger logger = Logger.getLogger(LoginService.class);

    @Autowired
    private GUserMapper userMapper;

    @Autowired
    TokenService tokenService;

    //验证员工Code登录
    public Response checkCode(String userId, HttpServletResponse re) {
        Response response = new Response();
        //验证携带此userId的员工在数据库中的距离是否为空
        try {
            GUser user = userMapper.findByUserGid(userId);
            if (user != null && user.getUserStatus().equals("1")) {
                response.setStatus(ResultHttpStatus.OK.getValue());
                response.setMsg(user.getUserName() + ":Login success");
            }
            //检查用户状态是否被禁用
            else if (user.getUserStatus().equals("0")) {
                response.setStatus(ResultHttpStatus.LOGIN_DENIED.getValue());
                response.setMsg("Login denied ");
            } else {
                response.setStatus(ResultHttpStatus.INTERNAL_ERROR.getValue());
                response.setMsg("Login failed ");
            }
            logger.info("Login" + response.getStatus() + "," + response.getMsg());
        } catch (Exception ex) {
            response.setStatus(ResultHttpStatus.INTERNAL_ERROR.getValue());
            response.setMsg("Login failed ");
            logger.info("Login failed" + ex);
        }
        return response;
    }

    //验证员工密码登录
    public Response checkPass(String userId, String pass, HttpServletResponse re) {
        Response response = new Response();
        try {
            Util util = new Util();
            String passMD5 = util.getMD5(pass);
            Map<String, Object> map = new HashMap<>();
            map.put("userGid", userId);
            map.put("userPassword", passMD5);
            GUser user = userMapper.findByUserGidAndUserPassword(map);
            if (user != null && user.getUserStatus().equals("1")) {
                response.setStatus(ResultHttpStatus.OK.getValue());
                response.setMsg(user.getUserName() + ":Login success");
                response.setData(tokenService.createNewToken(user.getUserName()));
            } else if (user.getUserStatus().equals("0")) {
                response.setStatus(ResultHttpStatus.LOGIN_DENIED.getValue());
                response.setMsg("Login denied ");
            } else {
                response.setStatus(ResultHttpStatus.INTERNAL_ERROR.getValue());
                response.setMsg("Login failed ");
            }
            logger.info("Login" + response.getStatus() + "," + response.getMsg());
        } catch (Exception ex) {
            response.setStatus(ResultHttpStatus.INTERNAL_ERROR.getValue());
            response.setMsg("Login failed ");
            logger.info("Login failed" + ex);
        }
        return response;
    }
}



